package com.xuanwei.core.validate;

import com.xuanwei.core.annos.Positive;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Author jiangyong
 * @Description TODO
 * @Date 2019/8/15 11:22
 **/
public class PositiveValidate implements ConstraintValidator<Positive, Integer> {

    private boolean containZero;

    @Override
    public void initialize(Positive constraintAnnotation) {
        this.containZero = constraintAnnotation.containZero();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if (value != null) {
            if (containZero && value >= 0) {
                return true;
            }
            if (!containZero && value > 0) {
                return true;
            }
        }
        return false;
    }
}
